package controller

import (
	"github.com/gin-gonic/gin"
	"yak.admin/src/entity"
	"yak.admin/src/middleware"
	"yak.admin/src/module/api/user/form"
	"yak.admin/src/repository"
	"yak.admin/src/util"
)

func Info(ctx *gin.Context)  {
	user := ctx.MustGet(util.UserKey).(entity.User)
	middleware.ResultSuccess(ctx, "", map[string]interface{}{
		"info": user.Info(),
	})
}

func Register(ctx *gin.Context)  {
	f := form.RegisterForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	if user, ok := f.Register(); ok {
		middleware.ResultSuccess(ctx, "", map[string]interface{}{
			"info": user.Info(), "token": user.Token,
		})
		return
	}
	middleware.ResultFail(ctx, "账号已注册", nil)
}

func Login(ctx *gin.Context)  {
	f := form.LoginForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	if user, err := f.Login(); err == nil {
		middleware.ResultSuccess(ctx, "", map[string]interface{}{
			"info": user.Info(), "token": user.Token,
		})
		return
	} else {
		middleware.ResultFail(ctx, err.Error(), nil)
	}
}

func ResetPassword(ctx *gin.Context)  {
	f := form.ResetPasswordForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	if _, err := f.ResetPassword(); err == nil {
		middleware.ResultSuccess(ctx, "新密码已保存", nil)
		return
	} else {
		middleware.ResultFail(ctx, err.Error(), nil)
	}
}

func SendSmsByGuest(ctx *gin.Context) {
	f := form.SmsForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	if err := f.SendCode(); err == nil {
		middleware.ResultSuccess(ctx, "发送成功！", nil)
	} else {
		middleware.ResultFail(ctx, err.Error(), nil)
	}
}

func ProfileAvatar(ctx *gin.Context)  {
	var f form.UserForm
	user := ctx.MustGet(util.UserKey).(entity.User)
	if filename, err := f.UploadAvatar(ctx, user); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	} else {
		user.Editor(map[string]interface{}{"avatar": filename})
		middleware.ResultSuccess(ctx,"", map[string]interface{}{ "filename": filename })
	}
}

// 修改昵称
func ProfileNickname(ctx *gin.Context) {
	f := form.NicknameForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"nickname": f.Nickname})
	middleware.ResultSuccess(ctx,"", nil)
}

// 用户名
func ProfileUsername(ctx *gin.Context) {
	f := form.UsernameForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"username": f.Username})
	middleware.ResultSuccess(ctx,"", nil)
}

// 性别
func ProfileSex(ctx *gin.Context) {
	f := form.SexForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"sex": f.Sex})
	middleware.ResultSuccess(ctx,"", nil)
}

// 年龄
func ProfileAge(ctx *gin.Context) {
	f := form.AgeForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"age": f.Age})
	middleware.ResultSuccess(ctx,"", nil)
}

// 更新单位名称
func ProfileCompany(ctx *gin.Context) {
	f := form.CompanyForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"company": f.Company})
	middleware.ResultSuccess(ctx,"", nil)
}

// 更新担任职务
func ProfileJob(ctx *gin.Context) {
	f := form.JobForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"job": f.Job})
	middleware.ResultSuccess(ctx,"", nil)
}

// 更新职级
func ProfileJobRank(ctx *gin.Context) {
	f := form.JobRankForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"job_rank": f.JobRank})
	middleware.ResultSuccess(ctx,"", nil)
}

// 更新所属地区
func ProfileAddress(ctx *gin.Context) {
	f := form.AddressFrom{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	user.Editor(map[string]interface{}{"province": f.Province, "city": f.City, "county": f.County, "address": f.Address})
	middleware.ResultSuccess(ctx,"", nil)
}

// 重置密码
func UpdatePassword(ctx *gin.Context) {
	f := form.EditPasswordForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	if err := f.Update(user); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	middleware.ResultSuccess(ctx,"", nil)
}

func Profile(ctx *gin.Context) {
	f := form.UserForm{}
	if err := util.DefaultGetValidParams(ctx, &f); err != nil {
		middleware.ResultFail(ctx, err.Error(), nil)
		return
	}
	user := ctx.MustGet(util.UserKey).(entity.User)
	if err := f.SetProfile(&user); err == nil {
		middleware.ResultSuccess(ctx, "", map[string]interface{}{
			"info": user.Info(),
		})
		return
	} else {
		middleware.ResultFail(ctx, err.Error(), nil)
	}
}

func PageConfig(ctx *gin.Context) {
	region := repository.RegionRepository{}
	data := make(map[string]interface{}, 4)
	data["province_labels"] = region.PickerLabelsByType(1)
	data["city_labels"] = region.PickerLabelsByType(2)
	data["county_labels"] = region.PickerLabelsByType(3)
	data["sex_labels"] = map[int]string {
		util.AllOfGender: "保密",
		util.MaleOfGender: "男",
		util.FemaleOfGender: "女",
	}
	middleware.ResultSuccess(ctx, "用户配置信息", data)
}
